﻿using System;
using System.Windows;
using System.Windows.Markup;
using GiveAQuiz.Common;

namespace GiveAQuiz.Common
{
  public static class QuestionHelper
  {

    public static DataTemplate GenerateDataTemplateForControl(Type controlType)
    {
      // Security limits us to not get part of the assembly name so
      // we'll hack it to cut off the assembly name after the first comma
      var assemblyName = controlType.Assembly.FullName.Split(',')[0];

      // Create a Data Template for our editor
      var xaml = string.Format(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                                     xmlns:my=""clr-namespace:{1};assemblyName={2}"">
                         <my:{0} />
                       </DataTemplate>",
                   controlType.Name, controlType.Namespace, assemblyName);
      return XamlReader.Load(xaml) as DataTemplate;
    }

    public static IQuestionType Copy(IQuestionType type)
    {
      return Activator.CreateInstance(type.GetType()) as IQuestionType;
    }


    public static IQuestionType CreateQuestionType(string typeName)
    {
      var type = Type.GetType(typeName);
      if (type != null)
      {
        return Activator.CreateInstance(type) as IQuestionType;
      }
      return null;
    }
  }
}
